<template>
  <div class="right-board">
    <div class="right_scrollbar_warp">
      <a
        v-if="activeFormItem.tag"
        class="document-link"
        target="_blank"
        :href="activeFormItem.document"
        title="查看组件文档"
      >
        <i class="el-icon-link" />
      </a>
      <!-- 组件属性 -->
      <div>
        <el-form
          label-position="top"
          label-width="80px"
          :model="activeFormItem"
        >
          <!-- 标签 -->
          <el-form-item label="标签:">
            <el-input
              placeholder="请输入标签名"
              v-model="activeFormItem.label"
            ></el-input>
          </el-form-item>
          <!-- 数据字段 -->
          <el-form-item label="数据字段:">
            <el-input
              placeholder="请输入数据字段"
              v-model="activeFormItem.model"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="输入框type:">
          <el-input v-model="activeFormItem.type"></el-input>
        </el-form-item> -->
          <!-- 占位内容 -->
          <el-form-item
            label="占位内容:"
            v-if="
              Object.keys(activeFormItem.attributes).includes('placeholder')
            "
          >
            <el-input
              placeholder="请输入占位内容"
              v-model="activeFormItem.attributes.placeholder"
            ></el-input>
          </el-form-item>
          <!-- 宽度 -->
          <!-- <el-form-item label="宽度:">
            <el-input
              placeholder="请输入宽度"
              v-model="activeFormItem.attributes.placeholder"
            ></el-input>
          </el-form-item>
        </el-form> -->
          <!-- 操作属性 -->
          <el-form-item label="操作属性:">
            <el-checkbox
              v-if="Object.keys(activeFormItem.attributes).includes('required')"
              v-model="activeFormItem.attributes['required']"
              @change="requiredChange"
              >必填</el-checkbox
            >
            <el-checkbox
              v-if="Object.keys(activeFormItem.attributes).includes('disabled')"
              v-model="activeFormItem.attributes.disabled"
              >禁用</el-checkbox
            >
            <el-checkbox
              v-if="
                Object.keys(activeFormItem.attributes).includes('clearable')
              "
              v-model="activeFormItem.attributes.clearable"
              >可清空</el-checkbox
            >
            <el-checkbox
              v-if="
                Object.keys(activeFormItem.attributes).includes('border') &&
                  activeFormItem.attributes.optionType === 'default'
              "
              v-model="activeFormItem.attributes.border"
              >边框</el-checkbox
            >
            <el-checkbox
              v-if="
                Object.keys(activeFormItem.attributes).includes(
                  'show-word-limit'
                )
              "
              v-model="activeFormItem.attributes['show-word-limit']"
              >字数统计</el-checkbox
            >
            <el-checkbox
              v-if="Object.keys(activeFormItem.attributes).includes('readonly')"
              v-model="activeFormItem.attributes.readonly"
              >只读</el-checkbox
            >
            <el-checkbox
              v-if="
                Object.keys(activeFormItem.attributes).includes('filterable')
              "
              v-model="activeFormItem.attributes.filterable"
              >可搜索
            </el-checkbox>
            <el-checkbox
              v-if="Object.keys(activeFormItem.attributes).includes('multiple')"
              v-model="activeFormItem.attributes.multiple"
              >多选
            </el-checkbox>
            <el-checkbox
              v-if="Object.keys(activeFormItem.attributes).includes('order')"
              v-model="activeFormItem.attributes.order"
              >显示边框
            </el-checkbox>
            <el-checkbox
              v-if="
                Object.keys(activeFormItem.attributes).includes('allow-half')
              "
              v-model="activeFormItem.attributes['allow-half']"
              >允许半选
            </el-checkbox>
            <el-checkbox
              v-if="
                Object.keys(activeFormItem.attributes).includes('show-text')
              "
              v-model="activeFormItem.attributes['show-text']"
              >辅助文字
            </el-checkbox>
            <el-checkbox
              v-if="
                Object.keys(activeFormItem.attributes).includes('show-score')
              "
              v-model="activeFormItem.attributes['show-score']"
              >当前分数
            </el-checkbox>
          </el-form-item>
          <el-form-item
            v-if="
              activeFormItem.slot &&
                Object.keys(activeFormItem.attributes).includes('format')
            "
            label="时间格式"
          >
            <el-input
              :value="activeFormItem.attributes.format"
              placeholder="请输入时间格式"
            />
          </el-form-item>
          <el-form-item
            v-if="
              activeFormItem.slot &&
                Object.keys(activeFormItem.attributes).includes(
                  'range-separator'
                )
            "
            label="分隔符"
          >
            <el-input
              v-model="activeFormItem.attributes['range-separator']"
              placeholder="请输入分隔符"
            />
          </el-form-item>
          <el-form-item
            v-if="
              activeFormItem.slot &&
                Object.keys(activeFormItem.attributes).includes(
                  'picker-options'
                )
            "
            label="时间段"
          >
            <el-input
              v-model="
                activeFormItem.attributes['picker-options'].selectableRange
              "
              placeholder="请输入时间段"
            />
          </el-form-item>
          <el-form-item
            label="前缀:"
            v-if="
              activeFormItem.slot &&
                Object.keys(activeFormItem.slot).includes('prepend')
            "
          >
            <el-input
              placeholder="请输入前缀"
              v-model="activeFormItem.slot.prepend"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="后缀:"
            v-if="
              activeFormItem.slot &&
                Object.keys(activeFormItem.slot).includes('append')
            "
          >
            <el-input
              placeholder="请输入后缀"
              v-model="activeFormItem.slot.append"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="最多输入:"
            v-if="Object.keys(activeFormItem.attributes).includes('maxlength')"
          >
            <el-input
              placeholder="请输入字符长度"
              v-model="activeFormItem.attributes.maxlength"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="最小行数:"
            v-if="
              activeFormItem.attributes.autosize &&
                Object.keys(activeFormItem.attributes.autosize).includes(
                  'minRows'
                )
            "
          >
            <el-input-number
              v-model="activeFormItem.attributes.autosize.minRows"
              :step="1"
              step-strictly
            ></el-input-number>
          </el-form-item>
          <el-form-item
            label="最大行数:"
            v-if="
              activeFormItem.attributes.autosize &&
                Object.keys(activeFormItem.attributes.autosize).includes(
                  'maxRows'
                )
            "
          >
            <el-input-number
              v-model="activeFormItem.attributes.autosize.maxRows"
              :step="1"
              step-strictly
            ></el-input-number>
          </el-form-item>

          <el-form-item
            v-if="activeFormItem.attributes.optionType"
            label="选项样式"
          >
            <el-radio-group v-model="activeFormItem.attributes.optionType">
              <el-radio-button label="default">
                默认
              </el-radio-button>
              <el-radio-button label="button">
                按钮
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
          <template
            v-if="
              activeFormItem.slot &&
                Object.keys(activeFormItem.slot).includes('options')
            "
          >
            <el-divider>选项</el-divider>
            <Vuedraggable
              :list="activeFormItem.slot.options"
              :animation="340"
              group="selectItem"
              handle=".option-drag"
            >
              <div
                v-for="(item, index) in activeFormItem.slot.options"
                :key="index"
                class="select-item"
              >
                <div class="select-line-icon option-drag">
                  <i class="el-icon-s-operation" />
                </div>
                <el-input
                  v-model="item.label"
                  placeholder="选项名"
                  size="small"
                />
                <el-input
                  placeholder="选项值"
                  size="small"
                  :value="item.value"
                />
                <div
                  class="close-btn select-line-icon"
                  @click="activeFormItem.slot.options.splice(index, 1)"
                >
                  <i class="el-icon-remove-outline" />
                </div>
              </div>
            </Vuedraggable>

            <div style="margin-left: 20px;">
              <el-button
                style="padding-bottom: 0"
                icon="el-icon-circle-plus-outline"
                type="text"
                @click="addSelectItem"
              >
                添加选项
              </el-button>
            </div>
          </template>

          <!-- 标签 -->
          <el-divider>正则校验</el-divider>
          <el-form-item label=" ">
            <template>
              <span v-for="(item, index) in activeFormItem.rules" :key="index">
                <div
                  class="reg-item"
                  v-if="!Object.keys(item).includes('required')"
                >
                  <span
                    class="close-btn"
                    @click="activeFormItem.rules.splice(index, 1)"
                  >
                    <i class="el-icon-close" />
                  </span>
                  <el-form-item label="表达式">
                    <el-input v-model="item.pattern" placeholder="请输入正则" />
                  </el-form-item>
                  <el-form-item label="错误提示" style="margin-bottom:0">
                    <el-input
                      v-model="item.message"
                      placeholder="请输入错误提示"
                    />
                  </el-form-item>
                </div>
              </span>
            </template>

            <div style="margin-left: 20px">
              <el-button
                icon="el-icon-circle-plus-outline"
                type="text"
                @click="addReg"
              >
                添加规则
              </el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import Vuedraggable from "vuedraggable";
export default {
  components: {
    Vuedraggable
  },
  props: {
    activeFormItem: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return { currentTab: "field" };
  },
  methods: {
    addReg() {
      this.activeFormItem.rules.push({
        pattern: "",
        message: ""
      });
    },
    requiredChange(val) {
      if (val) {
        this.activeFormItem.rules.push({
          required: true, // 必须填写
          message: "必填项"
        });
      } else {
        this.activeFormItem.rules.forEach((element, index) => {
          if (Object.keys(element).includes("required")) {
            this.activeFormItem.rules.splice(index, 1);
          }
        });
      }
    },
    addSelectItem() {
      this.activeFormItem.slot.options.push({
        label: "",
        value: ""
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.right_scrollbar_warp {
  position: relative;
  padding: 12px 18px 15px 15px;
}
.right-board {
  width: 270px;
  padding-top: 3px;
}

.right-scrollbar {
  padding: 12px 18px 15px 15px;
}
.select-item {
  display: flex;
  border: 1px dashed #fff;
  box-sizing: border-box;
  & .close-btn {
    cursor: pointer;
    color: #6A66F6;
  }
  & .el-input + .el-input {
    margin-left: 4px;
  }
}
.reg-item {
  padding: 12px 6px;
  background: #6A66F635;
  position: relative;
  border-radius: 4px;
  .close-btn {
    position: absolute;
    right: -6px;
    top: -6px;
    display: block;
    width: 16px;
    height: 16px;
    line-height: 16px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    color: #fff;
    text-align: center;
    z-index: 1;
    cursor: pointer;
    font-size: 12px;
    &:hover {
      background: rgba(210, 23, 23, 0.5);
    }
  }
  & + .reg-item {
    margin-top: 18px;
  }
}
.document-link {
  position: absolute;
  display: block;
  width: 26px;
  height: 26px;
  top: -14px;
  left: 0;
  cursor: pointer;
  background: #6A66F6;
  z-index: 1;
  border-radius: 0 0 6px 0;
  text-align: center;
  line-height: 26px;
  color: #fff;
  font-size: 18px;
}
</style>
